﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using Ession;

namespace PassGenerator
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        private void btn_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtPath.Text))
            {
                MessageBox.Show("选择生成文件");
                return;
            }
            generateAll = chkcheckBox1.Checked;
            saveFilePath = txtPath.Text;
            digitNumber = int.Parse(txttextBox1.Text);
            PassLength = int.Parse(textBox1.Text);

            ThreadPool.QueueUserWorkItem(Generate);
            interval = int.Parse(textBox2.Text);
            tmrtimer1.Interval = interval * 1000;
            tmrtimer1.Enabled = true;
            tmrtimer1_Tick(null, null);
        }

        private int interval;
        private long lastValue;

        private long computeSp;
        private bool generateAll;
        private int digitNumber;
        private int PassLength;
        private int min;
        private int max;
        private Ession.DigitPwdGenerator dpg;
        private long total = 0;
        void Generate(object o)
        {

            using (StreamWriter streamWriter = new StreamWriter(saveFilePath, true))
            {
                dpg = new DigitPwdGenerator();
                dpg.PassLength = PassLength;
                this.Invoke(
                    (Action)(() =>
                             {
                                 // prgprogressBar1.Maximum = (int)(dpg.MaxValue / 100);
                                 total = dpg.MaxValue;

                             }

                       ));
                dpg.DigitNumber = digitNumber;
                if (generateAll)
                {
                    dpg.GenerateUsingNextLower(streamWriter);
                    //dpg.GenerateAllLowerDigitNumber(streamWriter);

                }
                else
                {
                    dpg.GenerateUsingNext(streamWriter);
                }

                this.Invoke(
                        (Action)(() =>
                          {
                              tmrtimer1_Tick(null, null);
                              tmrtimer1.Enabled = false;
                              MessageBox.Show("Done!");
                          }

                           ));
            }
        }

        private string saveFilePath;
        private void btnbutton1_Click(object sender, EventArgs e)
        {
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                txtPath.Text = saveFileDialog1.FileName;
                saveFilePath = saveFileDialog1.FileName;
            }
        }

        private void tmrtimer1_Tick(object sender, EventArgs e)
        {
            if (dpg != null)
            {
                long cv = dpg.CurrentValue;
                computeSp = (long)(cv - lastValue) / interval;

                //  prgprogressBar1.Value = (int)(cv / 100);

                try
                {
                    lbCurrent.Text = string.Format("{0},进度{1:p},数字个数{2}.速度{3}个/s,已找到{4},预计剩余时间{5}",
                 cv.ToString(), (double)cv / (double)total,
                 dpg.DigitNumber, computeSp, dpg.FoundCount, TimeSpan.FromSeconds((total - cv) / computeSp));
                }
                catch (Exception)
                {
                    lbCurrent.Text = string.Format("{0},进度{1:p},数字个数{2}.速度{3}个/s,预计剩余时间{4}",
                0, 0,
                0, 0, "99:99:99");
                }

                lastValue = cv;
            }

        }

        private void btnMobile_Click(object sender, EventArgs e)
        {
            new frmMobile().Show();
        }

        private void btn1_Click(object sender, EventArgs e)
        {
            new frmPermutations().Show();
        }


    }

}
